package Games::Go::KGS;

use CGI::Simple;
use Moose;
use Data::Dumper;
use List::MoreUtils qw(zip natatime);
use LWP::Simple;
use HTML::TreeBuilder;
use XML::Writer;

our $VERSION = '0.01';

my $kgs = 'http://www.gokgs.com' ;
my $kgs_archive = "$kgs/gameArchives.jsp";

has 'user' => ( is => 'ro', required => 1 ) ;
has 'w' => ( is => 'rw' ) ;

has 'output' => (is => 'rw' );
has 'html' => (is => 'rw', lazy =>1, default => sub { LWP::Simple::get( $_[0]->final_url ) ; } ) ;
has 'tree' => (is => 'rw', lazy =>1, default => sub { HTML::TreeBuilder->new_from_content( $_[0]->html ) ; } ) ;
has 'parse' => (is => 'rw') ;

sub BUILD {
  my($self)=@_;

  my $output;

  $self->parse_page;

}

sub final_url {
    my($self)=@_;

    my $final_url="$kgs_archive?user=" . $self->user;
}
    

sub parse_page {
    my($self)=@_;

    my @table = $self->tree->look_down('_tag' => 'table');


    # --- Game List

    my $table = $table[0];

    my @potential_game = $table->look_down('_tag' => 'tr');


    my @viewable_game = grep {
	my $elem = $_;

	my $game_link = $elem->look_down(href => qr!files.gokgs.com/games!);

	if ($game_link) {
	    my $content = ($game_link->content_list)[0];
	    $content eq 'Yes';
	} else {
	    ();
	}

    } @potential_game;



    my @tag = qw(viewable white black setup start_time type result);


    my $U = \&as_url;
    my $P = \&as_player;
    my $H = \&as_html;
    my $C = \&as_content;
    my $T = \&as_text;

    my @print = ($U, $P, $P, $T, $C, $C, $C);

    my %ret;
    my @game;
    
    for my $game (@viewable_game) {

	my %output;

	my @td = $game->look_down('_tag' => 'td');
	fix_colspan($_) for @td;
	@td = $game->look_down('_tag' => 'td');

	my @zip = zip @tag, @td, @print ;
	my $it = natatime 3, @zip;



	while (my ($tag, $td, $print) = $it->()) {

	    $output{$tag} = $self->$print($td) ;
	}
  
	push @game, \%output;

    }
    $ret{games} = \@game;

    # --- Download Links

    my @link = $self->tree->look_down(href => qr!/servlet/archives!);
    my %href = ( zip => $kgs .  $link[0]->attr('href'),
		 tgz => $kgs .  $link[1]->attr('href')
		 ) ;

    $ret{download} = \%href;

    # --- Other archive pages

    my $period_table = $table[1];

    my @k;

    my @period;
    my @period_link = $period_table->look_down(_tag => 'a');
    
    for my $period_link (@period_link) {
	my $href = $kgs . $period_link->attr('href');
	my ($base, $query) = split qr/[?]/, $href;
	my $q = new CGI::Simple( $query ) ;
	push @period, { year => $q->param('year'),
			month => $q->param('month') } ;
    }

    $ret{period} = \@period;

    # - assign parse to attribute

    $self->parse(\%ret);

 
}


sub as_content {
  my($self, $elem)=@_;

  ($elem->content_list)[0];
}

sub as_url {
  my($self, $elem)=@_;

  my $a = $elem->look_down(href => qr/./);
  $a->attr('href');
}

sub as_player {
  my($self, $elem)=@_;

  my $a = $elem->look_down(_tag => 'a');

  {
      url => $kgs . '/' . $a->attr('href'),
	name => ($a->content_list)[0]
    } ;

}

# reset emacs mode


sub as_html {
  my($self, $elem)=@_;

  $elem->as_HTML;
}

sub as_text {
  my($self, $elem)=@_;

  $elem->as_text;
}



sub fix_colspan {
  my($td)=@_;

  my $colspan = $td->attr('colspan');
  
  if (defined($colspan) and $colspan == 2) {

    my $black_white = ($td->content_list)[0];
    my @a = $td->look_down(_tag => 'a');
    my ($black, $white) = @a[1,2];

    my @td = (HTML::Element->new_from_lol( [td => $black] ) );
    push @td, HTML::Element->new_from_lol( [td => $white] ) ;
    $td->replace_with(@td)->delete;
  }
}

sub games {
    my($self,$period)=@_;
}
    

sub current {
  
  my($self)=@_;

  

  my $user = $self->user;

  my $final_url="$kgs_archive?user=$user";

  my $table = $self->tree->look_down('_tag' => 'table');

  my @potential_game = $table->look_down('_tag' => 'tr');


  my @viewable_game = grep {
    my $elem = $_;

    my $game_link = $elem->look_down(href => qr!files.gokgs.com/games!);

    if ($game_link) {
      my $content = ($game_link->content_list)[0];
      $content eq 'Yes';
    } else {
      ();
    }

  } @potential_game;



  my @tag = qw(viewable white black setup start_time type result);


  my $U = \&as_url;
  my $P = \&as_player;
  my $H = \&as_html;
  my $C = \&as_content;
  my $T = \&as_text;

  my @print = ($U, $P, $P, $T, $C, $C, $C);



  $self->w->startTag('gamelist');


  for my $game (@viewable_game) {

    my @td = $game->look_down('_tag' => 'td');
    fix_colspan($_) for @td;
    @td = $game->look_down('_tag' => 'td');

    my @zip = zip @tag, @td, @print ;
    my $it = natatime 3, @zip;

    $self->w->startTag('game');

    while (my ($tag, $td, $print) = $it->()) {

      #warn $td->as_HTML;

      $self->w->startTag($tag);
      $self->$print($td) ;
      $self->w->endTag();

    }
  
    $self->w->endTag('game');


  }

  $self->w->endTag;
 

  ${$self->output};

}




1;				# End of Games::Go::KGS
